package com.ding.easycontroller.file.impl;

import com.ding.easycontroller.file.ControllerFile;
import com.ding.easycontroller.file.ScriptFilePathHolder;
import com.ding.easycontroller.utils.ElementUtils;
import com.ding.easycontroller.work.Controller;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

@Component
public class ControllerFileImpl implements ControllerFile {


    @Autowired
    private XMLOutputter docWriter;

    private File root;

    //创建文件路径
    ControllerFileImpl(ScriptFilePathHolder holder) {
        File parent = holder.getRoot();
        File file = new File(parent, "controller");
        if (!file.exists() && !file.mkdirs()) {
            throw new RuntimeException("路径创建失败" + file.getAbsolutePath());
        }
        this.root = file;
    }

    private String fileName(String id) {
        return id + ".controller";
    }


    private Document controller2Document(Controller controller) {
        Document document = new Document();
        Element root = new Element("controller");

        //作为脚本添加元素
        ScriptFile.scriptToElement(controller, root);

        //uri
        Element uri = new Element("uri");
        uri.setText(controller.getUri());
        root.getChildren().add(uri);

        //请求方法
        Element method = new Element("method");
        method.setText(controller.getMethod());
        root.getChildren().add(method);

        document.setRootElement(root);

        return document;
    }

    @Override
    public void add(Controller controller) {

        try {

            Document document = controller2Document(controller);

            String strFromXml = docWriter.outputString(document);

            //写入文件
            FileWriter writer = new FileWriter(new File(this.root, fileName(controller.getId())));

            writer.write(strFromXml);
            writer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void removeById(String id) {
        new File(fileName(id)).delete();
    }

    @Override
    public List<Controller> listAll() {
        List<Controller> all = new ArrayList<>();
        File[] files = root.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".controller")) {
                    Element element = ElementUtils.readElement(file);
                    if (element != null && element.getName().equals("controller")) {
                        Controller controller = new Controller();

                        //作文脚本元素进行复原
                        ScriptFile.elementToScript(element, controller);

                        controller.setUri(element.getChild("uri").getText());
                        controller.setMethod(element.getChild("method").getText());

                        all.add(controller);

                    }
                }
            }
        }
        return all;

    }

    @Override
    public void update(Controller controller) {
        try {
            Document document = controller2Document(controller);
            String strFromXml = docWriter.outputString(document);
            FileWriter writer = new FileWriter(new File(this.root, fileName(controller.getId())));
            writer.write(strFromXml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
